/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gclient;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author sajin
 */
public class Dispatcher implements Runnable {
	public Dispatcher(int listeningPort, GalaxiaClientUI galaxiaClientUI) {
		this.listeningPort = listeningPort;
		listenerThread = new Thread(this, "Dispatcher");
		this.galaxiaClientUI = galaxiaClientUI;
		quit = false;
		listenerThread.start();
	}

	@Override
	protected void finalize() throws Throwable {
		quit = false;
		super.finalize();
	}

	@Override
	public void run() {
		while( !quit ) {
			try {
				ServerSocket serverSocket = new ServerSocket(listeningPort);

				while (true) {
					Socket client = serverSocket.accept();
					System.out.println("Accepted Connection");
					new Announcer(client, galaxiaClientUI);
				}
			} catch (IOException ex) {
				System.out.println(" Error Occured " + ex.getMessage());
			}
		}
	}

	protected Thread listenerThread;
	protected int listeningPort;
	protected boolean quit;
	protected GalaxiaClientUI galaxiaClientUI;
}
